﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace IKEA.FileExchange.Domain.FilePackage
{
    public static class FilePackageInformationProvider
    {
        public static FilePackageInformation Extract(SPListItem listItem)
        {
            FilePackageInformation packageInformation = new FilePackageInformation();

            packageInformation.IsConfidential = bool.Parse(listItem[FilePackageFieldNames.IsConfidential].ToString());
            packageInformation.RecipientsRaw = listItem[FilePackageFieldNames.FilePackageRecipients] as string;
            packageInformation.SendManagementLinkToEmail = listItem[FilePackageFieldNames.UploadPackageManagementEmail] as string;
            packageInformation.UploadComments = listItem[FilePackageFieldNames.UploadComments] as string;

            return packageInformation;
        }

        public static FilePackageInformation GetByUniqueId(Guid uniqueId)
        {
            FilePackageInformation info = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        SPList spList = IKEAFileStoreHelper.GetFilePackagesList(spWeb);

                        SPListItem listItem = spList.GetItemByUniqueId(uniqueId);

                        info = Extract(listItem);
                    }
                }
            }
            );

            return info;
        }
    }
}
